<template>
  <div>
    <el-form :model="form" ref="form" v-if="showComment" inline label-width="100px">
      <el-form-item label="审批意见" prop="comment">
        <el-input class="form-control" type="textarea" :rows="3" placeholder="请填写审批意见" v-model="form.comment"></el-input>
      </el-form-item>
    </el-form>
    <div v-if="processList.length">
      <el-table :data="processList" border fit highlight-current-row style="width: 100%">
        <el-table-column label="任务名称" prop="taskName" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="到达时间" prop="beginDate" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="完成时间" prop="endDate" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="处理者" prop="assigneeName" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="处理意见" prop="comment" align="center" show-overflow-tooltip></el-table-column>
      </el-table>
    </div>
    <el-footer class="footer-container fixed-button">
      <el-button @click="back()">返回</el-button>
      <el-button type="primary" @click="save" v-if="buttons.save.show">{{buttons.save.name}}</el-button>
      <el-button type="primary" @click="submit" v-if="buttons.submit.show">{{buttons.submit.name}}</el-button>
      <el-button type="primary" @click="claim" v-if="buttons.claim.show">{{buttons.claim.name}}</el-button>
      <el-button type="primary" @click="agree(buttons.agree.save)" v-if="buttons.agree.show">{{buttons.agree.name}}</el-button>
      <el-button type="primary" @click="reject(buttons.reject.save)" v-if="buttons.reject.show">{{buttons.reject.name}}</el-button>
      <el-button type="primary" @click="stop" v-if="buttons.stop.show">{{buttons.stop.name}}</el-button>
      <el-button type="primary" v-if="bizKey" @click="processGraphView">流程查看</el-button>
    </el-footer>
    <el-dialog title="流程图" :visible.sync="processGraphVisible" :fullscreen="true" append-to-body custom-class="large">
      <div class="process-graph">
        <img :src="imageUrl" class="process-image">
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { pass, unPass, claim, stopProcess, historyFlow, processImage, getProcessInfo } from '@/api/base/process'
export default {
  name: 'workflow',
  props: {
    procDefKey: {
      type: String
    },
    bizKey: {
      type: String
    },
    showComment: {
      type: Boolean,
      default: true
    },
    showHistory: {
      type: Boolean,
      default: true
    },
    showSaveBtn: {
      type: Boolean,
      default: true
    },
    showSubmitBtn: {
      type: Boolean,
      default: true
    },
    operable: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      form: {
        comment: undefined
      },
      processList: [],
      processGraphVisible: false,
      imageUrl: undefined,
      buttons: {
        save: {
          name: undefined,
          show: false,
          save: false
        },
        submit: {
          name: undefined,
          show: false,
          save: false
        },
        claim: {
          name: undefined,
          show: false,
          save: false
        },
        agree: {
          name: undefined,
          show: false,
          save: false
        },
        reject: {
          name: undefined,
          show: false,
          save: false
        },
        stop: {
          name: undefined,
          show: false,
          save: false
        }
      },
      task: {
        procInsId: undefined,
        taskId: undefined,
        taskDefKey: undefined,
        map: undefined
      }
    }
  },
  methods: {
    init () {
      this.$nextTick(() => {
        if (this.operable) {
          this.initButtons()
        }
        if (this.showHistory) {
          this.history()
        }
      })
    },
    initButtons () {
      this.buttons.save.show = this.showSaveBtn
      this.buttons.save.name = '保存'
      this.buttons.submit.show = this.showSubmitBtn
      this.buttons.submit.name = '提交'
      if (!this.bizKey) return
      const param = {}
      param.bizKey = this.bizKey
      param.userId = this.$store.getters.id
      this.$emit('loading-start')
      getProcessInfo(param).then(response => {
        if (response.data && response.data.buttons && response.data.buttons.length) {
          response.data.buttons.forEach(el => {
            this.buttons[el.type].show = true
            this.buttons[el.type].name = el.name
            if (response.data.task) {
              this.buttons[el.type].save = response.data.task.isFormSave
            }
          })
          if (response.data.task) {
            this.task.procInsId = response.data.task.processInstanceId
            this.task.taskId = response.data.task.id
            this.task.taskDefKey = response.data.task.taskDefinitionKey
            this.buttons.submit.show = false
            this.$emit('set-task-info', this.task)
          }
        }
        this.$emit('loading-end')
      }).catch(error => {
        this.$emit('loading-end')
        console.error(error)
      })
    },
    back () {
      this.$emit('back')
    },
    save () {
      this.$emit('save')
    },
    submit () {
      this.$emit('submit', this.form.comment)
    },
    claim () {
      this.$emit('loading-start')
      claim(this.task.taskId, this.$store.getters.id).then(() => {
        this.$notify({
          title: '提示',
          type: 'success',
          message: '签收成功!',
          duration: 2000
        })
        this.$emit('loading-end')
        this.resetButtons()
        this.initButtons()
      }).catch(error => {
        this.$emit('loading-end')
        console.error(error)
      })
    },
    agree (isSave) {
      if (isSave) {
        this.$emit('agree', this.form.comment)
      } else {
        this.pass()
      }
    },
    reject (isSave) {
      if (!this.form.comment) {
        this.$alert('请填写审批意见', '提示', {
          type: 'warning'
        })
        return
      }
      this.task.comment = this.form.comment
      if (isSave) {
        this.$emit('reject', this.form.comment)
      } else {
        this.unpass()
      }
    },
    unpass () {
      this.$emit('loading-start')
      unPass(this.task).then(() => {
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
        this.$emit('loading-end')
        this.back()
      }).catch(error => {
        this.$emit('loading-end')
        console.error(error)
      })
    },
    stop () {
      this.$emit('stop')
    },
    stopProcess () {
      this.task.comment = this.form.comment
      this.$emit('loading-start')
      stopProcess(this.task).then(() => {
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
        this.$emit('loading-end')
        this.back()
      }).catch(error => {
        this.$emit('loading-end')
        console.error(error)
      })
    },
    pass (map) {
      if (map) {
        this.task.map = map
      }
      this.task.comment = this.form.comment
      this.$emit('loading-start')
      pass(this.task).then(() => {
        this.$notify({
          title: '成功',
          message: '审批成功',
          type: 'success',
          duration: 2000
        })
        this.$emit('loading-end')
        this.back()
      }).catch(error => {
        this.$emit('loading-end')
        console.error(error)
      })
    },
    history () {
      if (!this.bizKey) return
      this.$emit('loading-start')
      historyFlow(this.procDefKey, this.bizKey).then(response => {
        this.processList = response
        this.$emit('loading-end')
      }).catch(error => {
        this.$emit('loading-end')
        console.error(error)
      })
    },
    processGraphView () {
      this.$emit('loading-start')
      processImage(this.procDefKey, this.bizKey).then(data => {
        this.imageUrl = data
        this.processGraphVisible = true
        this.$emit('loading-end')
      }).catch(error => {
        this.$emit('loading-end')
        console.error(error)
      })
    },
    reset () {
      this.resetButtons()
      this.processList = []
      this.form = {
        comment: undefined
      }
      this.imageUrl = undefined
      this.task = {
        procInsId: undefined,
        taskId: undefined,
        taskDefKey: undefined,
        map: undefined
      }
    },
    resetButtons () {
      this.buttons = {
        save: {
          name: undefined,
          show: false,
          save: false
        },
        submit: {
          name: undefined,
          show: false,
          save: false
        },
        claim: {
          name: undefined,
          show: false,
          save: false
        },
        agree: {
          name: undefined,
          show: false,
          save: false
        },
        reject: {
          name: undefined,
          show: false,
          save: false
        },
        stop: {
          name: undefined,
          show: false,
          save: false
        }
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.form-control {
  width: 910px;
}
.footer-container {
  margin: 10px;
}
.process-graph {
  text-align: center;
}
.process-image {
  display: inline-block;
  vertical-align: middle;
  margin: 0px 0px 50px 20px;
}
</style>
